package com.hfb.mashibing.alip8.spring.controller;

import com.hfb.mashibing.alip8.spring.component.MyHttpSessionListener;
import com.hfb.mashibing.alip8.spring.entity.Person;
import com.hfb.mashibing.alip8.spring.mapper.PersonMapper;
import javax.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class SayRestController {

    @Value("${random.int(10)}")
    private int random;
    @Autowired
    private PersonMapper personMapper;


    @GetMapping("say")
    public String say(HttpSession session){
        log.debug("say");

        session.setAttribute("user.name", "hfb"+random);

        return "Hello world";
    }
    @GetMapping("online")
    public String online(){
        log.debug("online");

        return "当前在线人数："+ MyHttpSessionListener.online +" 人";
    }
    @RequestMapping("all")
    public Object all(HttpSession session){
        log.debug("all");

        session.setAttribute("user.name", "hfb"+random);

        return personMapper.getAll();
    }
    @RequestMapping("ia")
    public Object initadmin(HttpSession session){
        log.debug("initadmin");

        session.setAttribute("user.name", "hfb"+random);

        personMapper.add(new Person(-1,"admin",35,"admin"));

        return personMapper.getAll();
    }
}
